import { createStore } from 'vuex'

const store = createStore({
  state: {
    isLoggedIn: false,
    user: null,
    stats: null,
    recommendations: []
  },
  mutations: {
    login(state, user) {
      state.isLoggedIn = true
      state.user = user
    },
    logout(state) {
      state.isLoggedIn = false
      state.user = null
    },
    setStats(state, stats) {
      state.stats = stats
    },
    setRecommendations(state, recommendations) {
      state.recommendations = recommendations
    }
  },
  actions: {
    async login({ commit }, { username, password }) {
      return new Promise((resolve) => {
        setTimeout(() => {
          if (username === "admin" && password === "123456") {
            commit('login', { 
              id: 1, 
              name: "管理员",
              username: "admin",
              interests: ["科技", "旅行", "美食"]
            })
            resolve(true)
          } else {
            resolve(false)
          }
        }, 1000)
      })
    },
    async fetchStats({ commit }) {
      return new Promise((resolve) => {
        setTimeout(() => {
          const stats = [
            { title: '总发文', value: '1.2k' },
            { title: '总点赞', value: '24.5k' },
            { title: '热点数', value: '56' },
            { title: '参与率', value: '78%' }
          ]
          commit('setStats', stats)
          resolve()
        }, 800)
      })
    },
    async fetchRecommendations({ commit }) {
      return new Promise((resolve) => {
        setTimeout(() => {
          const recommendations = [
            {
              id: 1,
              username: "旅行达人",
              content: "今天发现了一个超棒的旅行地点，推荐给大家！",
              likes: 120,
              comments: 35,
              shares: 18
            },
            {
              id: 2,
              username: "科技前沿",
              content: "最新AI技术将改变我们的生活，大家怎么看？",
              likes: 450,
              comments: 89,
              shares: 42
            },
            {
              id: 3,
              username: "美食家",
              content: "分享一道简单又美味的家常菜做法，快来试试吧！",
              likes: 320,
              comments: 45,
              shares: 27
            }
          ]
          commit('setRecommendations', recommendations)
          resolve()
        }, 800)
      })
    }
  }
})

export default store